1
|
|
|
/** global: UB */ |
2
|
|
|
|
3
|
|
|
//removeIf(nodejs) |
4
|
|
|
|
5
|
|
|
/* Async Utils - NodeJS only */ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Uses the waterfall async pattern to loop a certain number of times. |
9
|
|
|
* |
10
|
|
|
* @param {Number} count Number of times to loop |
11
|
|
|
* @param {*Function} iterator Called with the array index per iteration |
12
|
|
|
* @param {*Function} complete Called when all iterations done |
13
|
|
|
*/ |
14
|
|
|
UB.asyncForEach = function(list, iterator, complete) { |
15
|
|
|
|
16
|
|
|
// quickly exit if no data |
17
|
|
|
if (list == null || list.length === 0){ |
18
|
|
|
complete(); |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// start of the loop |
23
|
|
|
var nextItemIndex = 0; //keep track of the index of the next item to be processed |
24
|
|
|
|
25
|
|
|
function report() { |
26
|
|
|
|
27
|
|
|
nextItemIndex++; |
28
|
|
|
|
29
|
|
|
// if nextItemIndex equals the number of items in list, then we're done |
30
|
|
|
if(nextItemIndex === list.length){ |
31
|
|
|
complete(); |
32
|
|
|
}else{ |
33
|
|
|
// otherwise, call the iterator on the next item |
34
|
|
|
iterator(list[nextItemIndex], report); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// instead of starting all the iterations, we only start the 1st one |
39
|
|
|
iterator(list[0], report); |
40
|
|
|
}; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Uses the waterfall async pattern to loop through the given list asynchronously. |
44
|
|
|
* |
45
|
|
|
* @param {number} count Total number of iterations |
46
|
|
|
* @param {*Function} iterator Called with the array value per iteration |
47
|
|
|
* @param {*Function} complete Called when all iterations done |
48
|
|
|
*/ |
49
|
|
|
UB.asyncFor = function(count, iterator, complete) { |
50
|
|
|
|
51
|
|
|
// quickly exit if no iterations |
52
|
|
|
if (count<=0){ |
53
|
|
|
complete(); |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// start of the loop |
58
|
|
|
var nextItemIndex = 0; //keep track of the index of the next item to be processed |
59
|
|
|
|
60
|
|
|
function report() { |
61
|
|
|
|
62
|
|
|
nextItemIndex++; |
63
|
|
|
|
64
|
|
|
// if nextItemIndex equals the number of items in list, then we're done |
65
|
|
|
if(nextItemIndex === count){ |
66
|
|
|
complete(); |
67
|
|
|
}else{ |
68
|
|
|
// otherwise, call the iterator on the next item |
69
|
|
|
iterator(nextItemIndex, report); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// instead of starting all the iterations, we only start the 1st one |
74
|
|
|
iterator(0, report); |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
//endRemoveIf(nodejs) |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
UB.newArray = function(value, size) { |
81
|
|
|
var values = []; |
82
|
|
|
|
83
|
|
|
if (size <= 0) { |
84
|
|
|
return values; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (value.isPrimitive()) { |
88
|
|
|
|
89
|
|
|
// PRIMITIVES |
90
|
|
|
for (var i = 0; i < size; i++) { |
91
|
|
|
values[i] = value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
}else { |
95
|
|
|
|
96
|
|
|
// NON PRIMITIVES |
97
|
|
|
for (var i = 0; i < size; i++) { |
|
|
|
|
98
|
|
|
values[i] = value.deepClone(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return values; |
103
|
|
|
}; |
This check looks for variables that are declared in multiple lines. There may be several reasons for this.
In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.
If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.